//
//  ViewController.m
//  09-HomeWork
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "ZoneViewController.h"
#define kTableViewCellReuse @"reuseIdentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *provinceAndCityOftableView;
@property (nonatomic, strong) NSMutableArray *allProArray;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];
    self.navigationItem.title = @"省份-市区";
}
#pragma mark - 数据处理
- (void)handleData {
    // 取到地址
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-HomeWork2/09-HomeWork2/area.txt";
    // 取到字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 取出(根据"\n")截取的所有的字符串, 放入数组中
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    // 1. 省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
    self.allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [_allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [_allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[_allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
}
#pragma mark - 创建tableView
- (void)createTableView {
    self.provinceAndCityOftableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_provinceAndCityOftableView];
    _provinceAndCityOftableView.delegate = self;
    _provinceAndCityOftableView.dataSource = self;
}
#pragma mark - 实现协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Province *pro = [_allProArray objectAtIndex:section];
    return pro.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _allProArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewCellReuse];
    }
    Province *pro = [_allProArray objectAtIndex:indexPath.section];
    City *city = [pro.array objectAtIndex:indexPath.row];
    NSArray *arr = [city.name componentsSeparatedByString:@" "];
    cell.textLabel.text = arr[2];
    cell.detailTextLabel.text = arr[3];
    return cell;
}
#pragma mark - TableView  header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    Province *pro = [_allProArray objectAtIndex:section];
    NSArray *arr = [pro.name componentsSeparatedByString:@" "];
    return arr[0];
}
#pragma mark - 选中cell时触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZoneViewController *zone = [[ZoneViewController alloc] init];
       Province *pro = [_allProArray objectAtIndex:indexPath.section];
    City *city = [pro.array objectAtIndex:indexPath.row];
    zone.arrOfZone = [NSArray arrayWithArray:city.array];
    NSArray *arr = [city.name componentsSeparatedByString:@" "];
    zone.navigationItem.title = arr[2];
    [self.navigationController pushViewController:zone animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
